package com.vip.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vip.commonutils.JwtUtils;
import com.vip.commonutils.MD5;
import com.vip.educenter.entity.UcenterMember;
import com.vip.educenter.entity.vo.RegisterVo;
import com.vip.educenter.mapper.UcenterMemberMapper;
import com.vip.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author hvip
 * @since 2020-08-31
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    // 登录方法
    @Override
    public String login(UcenterMember member) {

        String mobile = member.getMobile();
        String password = member.getPassword();

        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password))
            return null;

        // 判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);

        // 判断查出来的对象是否为空
        if (mobileMember == null)
            return null;

        // MD5加密后再判断
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            return null;
        }

        if (mobileMember.getIsDisabled()) {
            return null;
        }

        // 登陆成功,生成token
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());


        return jwtToken;
    }

    // 注册的方法
    @Override
    public void register(RegisterVo registerVo) {

        // 获取注册的数据
        String code = registerVo.getCode(); //验证码
        String mobile = registerVo.getMobile(); // 手机号
        String password = registerVo.getPassword(); // 密码
        String nickname = registerVo.getNickname(); // 昵称

        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname))
            return ;

        // 判断发送的短信验证码
        // 获取redis的密码
        String redisCode = redisTemplate.opsForValue().get(mobile);

        if (!code.equals(redisCode))
            return;

        // 判断手机号是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            return;
        }

        // 添加
        UcenterMember member = new UcenterMember();

        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);        //用户是否禁用
        member.setAvatar("https://edu-guli-hvip.oss-cn-chengdu.aliyuncs.com/teacher/1442297999141.jpg");
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    // 根据日期查询
    @Override
    public Integer coutRegisterDay(String day) {
        return baseMapper.coutRegisterDay(day);
    }


}
